<template>
  <!-- 车辆列表页 -->
  <div v-loading="isLoading">
    <!-- 筛选表单 -->
    <el-form :model="form" label-width="80px">
      <el-form-item label="车辆类型">
        <el-select v-model="form.truckTypeId" placeholder="请选择">
          <el-option value="1575066321985286145" label="4.2米厢式货车" />
          <el-option value="1575257668885229569" label="6.8米厢式货车" />
          <el-option value="1575257460197634049" label="12.8米厢式货车" />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆号码">
        <el-input v-model="form.licensePlate" />
      </el-form-item>

      <el-form-item>
        <el-button type="danger" @click="seekFn">搜索</el-button>
        <el-button @click="resetFn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 筛选按钮 -->
    <div class="sxan">
      <span @click="all"><a>全部</a></span>
      <span @click="usable(1)"><a>可用</a></span>
      <span @click="blockupFn(0)"><a>停用</a></span>
    </div>
    <!-- 下方列表 -->
    <el-button type="danger" @click="addcargps">新增车辆</el-button>
    <el-table :data="carList" style="width: 100%">
      <!-- el-table-column 列：通过prop绑定数据中的键 -->
      <!-- 列名：通过label来设置 -->
      <el-table-column type="index" :index="indexFn" label="序号" width="80" />
      <el-table-column prop="licensePlate" label="车牌号码" width="200" />
      <el-table-column prop="truckTypeName" label="车辆类型" width="150" />
      <el-table-column prop="driverNum" label="司机数量" width="100" />
      <el-table-column
        prop="workStatus"
        label="车辆状态"
        :formatter="formatterFn"
        width="120"
      />
      <el-table-column prop="deviceGpsId" label="GPS设备ID" width="150" />
      <el-table-column prop="allowableLoad" label="实载重量(T)" width="100" />
      <el-table-column
        prop="allowableVolume"
        label="实载体积(方)"
        width="100"
      />
      <el-table-column label="操作" width="300">
        <template #default="{ row }">
          <el-button
            type="text"
            size="small"
            @click="$router.push(`/car/detail/${row.id}`)"
          >
            查看详情
          </el-button>
          <el-button type="text" size="small" style="color: red">
            启用
          </el-button>
          <el-button type="text" size="small"> 配置司机 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <addCargpsVue ref="addcargps" :show-dialog.sync="showDialog" />
  </div>
</template>
<script>
import { getCarPageApi } from '@/api/car'
import addCargpsVue from './components/add-cargps.vue'
export default {
  components: {
    addCargpsVue
  },
  data() {
    return {
      isLoading: false,
      // 表单
      form: {
        truckTypeId: '', // 车辆类型
        licensePlate: '' // 车牌号码
      },

      carList: [], // 车辆列表（全部）
      total: 0, // 总条数
      page: 1, // 当前页码
      pageSize: 3, // 每页的数据条数
      showDialog: false
    }
  },
  created() {
    this.getCarList()
  },
  methods: {
    async getCarList(truckTypeId, licensePlate, workStatus) {
      this.isLoading = true
      const res = await getCarPageApi(
        this.page,
        this.pageSize,
        truckTypeId,
        licensePlate,
        workStatus
      )
      this.total = Number(res.data.data.counts)
      this.carList = res.data.data.items
      this.isLoading = false
    },

    formatterFn(row, column, cellValue) {
      if (cellValue === 1 || cellValue === '1') {
        return '可用'
      } else if (cellValue === 0 || cellValue === '0') {
        return '停用'
      }
      return cellValue
    },
    // 搜索
    seekFn() {
      this.getCarList(this.form.truckTypeId, this.form.licensePlate)
    },
    // 重置
    resetFn() {
      this.form = {
        truckTypeId: '', // 车辆类型
        licensePlate: '' // 车牌号码
      }
    },
    // 全部
    all() {
      this.getCarList(this.form.truckTypeId, this.form.licensePlate)
    },
    // 可用
    usable(workStatus) {
      this.getCarList(this.form.truckTypeId, this.form.licensePlate, workStatus)
    },
    // 停用
    blockupFn(workStatus) {
      this.getCarList(this.form.truckTypeId, this.form.licensePlate, workStatus)
    },
    // 新增
    addcargps() {
      this.showDialog = true
    },

    // 序号
    indexFn(index) {
      return index + 1 + (this.page - 1) * this.pageSize
    },
    // 每页条数变化了，切换了每页的条数
    handleSizeChange(val) {
      // 切换了每页几条数据，跳回到第一页
      this.page = 1
      this.pageSize = val

      this.getCarList()
      // console.log(`每页 ${val} 条`)
    },
    // 当前页变化，改变当前页触发执行
    handleCurrentChange(val) {
      this.page = val
      this.getCarList()
      // console.log(`当前页: ${val}`)
    }
  }
}
</script>
<style scoped lang="scss">
.el-form {
  margin-top: 20px;
  padding-top: 10px;
  height: 60px;
  width: 100%;
  background-color: rgb(255, 246, 247);
  .el-form-item {
    float: left;
    width: 400px;
  }
}
.sxan {
  margin-top: 20px;
  width: 100%;
  height: 60px;
  // padding: 0;
  // padding-top: 100px;
  background-color: rgb(255, 237, 237);
  margin-bottom: 30px;
  span {
    text-align: center;
    line-height: 40px;
    padding: 0;
    float: left;
    margin: 10px 20px;
    width: 100px;
    height: 40px;
    background-color: #fff;
  }
}
</style>
